#include <algorithm>
#include <cstdio>
#include <tuple>
constexpr int MaxN = 1e5 + 5;
int n;
long long dp[2][MaxN];
std::tuple<long long, long long> v[MaxN];
int main()
{
    freopen("boast.in", "r", stdin);
    freopen("boast.out", "w", stdout);
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
    {
        long long x, y;
        scanf("%lld%lld", &x, &y);
        v[i] = {x, y};
    }
    std::sort(v + 1, v + 1 + n,
              [](const std::tuple<long long, long long> &x, const std::tuple<long long, long long> &y) -> bool {
                  return std::get<1>(x) < std::get<1>(y);
              });
    long long min = std::get<0>(v[1]);
    for (int i = 2; i <= n; i++)
    {
        dp[0][i] = min + std::get<0>(v[i]) + std::get<1>(v[i]);
        min = std::min(min, std::get<0>(v[i]));
    }
    min = dp[0][2];
    long long answer = 0x7f7f7f7f7f7f7f7f;
    for (int i = 3; i <= n; i++)
    {
        dp[1][i] = min + std::get<0>(v[i]) + std::get<1>(v[i]);
        min = std::min(min, dp[0][i]);
        answer = std::min(answer, dp[1][i]);
    }
    printf("%lld\n", answer);
    return 0;
}